import axios from 'axios'
import router from '../router/index'
import { Notification, MessageBox, Message } from 'element-ui'



const CancelToken = axios.CancelToken
const source = CancelToken.source()
const service = axios.create({
  baseURL: 'http://localhost:8081',
  timeout: 3000,
  
})

//设置cross跨域 并设置访问权限 允许跨域携带cookie信息,使用JWT可关闭
service.defaults.withCredentials = false;



service.interceptors.request.use(config => {
  // console.log(localStorage.getItem('tokenStr'))
  // if (localStorage.getItem('tokenStr')) {
    
  //   config.headers['Authorization'] = localStorage.getItem('tokenStr')
  // }
  // 判断是否存在token，如果存在的话，则每个http header都加上token
  let token = localStorage.getItem('tokenStr')
  let tokenHead = localStorage.getItem('tokenHead')
  if (token) {
   
    //config.headers['Authorization'] = token
    config.headers.Authorization = tokenHead + token+'';
    
  }
  
  return config
}, error => {

})

service.interceptors.response.use(res => {
  
  //console.log(res)
  if (res.headers['content-type'] === 'application/octet-stream') {
    return res
  }
  let result = res.data
  console.log(res)
  if (res.data.code === 2003 || res.data.code === 2008 || res.data.code === 2005
    || res.data.code === 2010 || res.data.code === 2501 || res.data.code === 3001
    || res.data.code === 3007 || res.data.code === 3008 || res.data.code === 3009 ) {
      Message({
        message: res.data.message,
        type:'error'
      })
      return result;
  
  } else if (res.data.code === 2001 || res.data.code === 2004) {
    //登陆过期或密码过期
    localStorage.setItem('logout', '1')
    Message({
      message: res.data.message,
      type:'error'
    })
    logout()
    return result;
  } else if (res.data.code === 2009) {
    //被挤下线
    MessageBox.confirm(res.data.message, '系统提示', {
        confirmButtonText: '重新登录',
        cancelButtonText: '退出',
        type: 'warning'
      }).then(action => {
        localStorage.setItem('logout', '1')
        if (action === 'confirm') {
          localStorage.removeItem('tokenStr')
          router.push('/login')
        }
    }).catch(err => {
      localStorage.setItem('logout', '0')
      if (err === 'cancel') {
        localStorage.removeItem('tokenStr')
        router.push('/login')
      }
    })
  } else if (res.data.code === 200) {
    console.log(result)
    return result;
  } else {
    return result;
  }
}, error => {
  return Promise.reject(error)
})

const logout = () => {
 
  if (localStorage.getItem('logout') === '1') {
  //  vm.$message.confirm('登录状态或密码已过期，请重新登录', '系统提示', {
  //       confirmButtonText: '重新登录',
  //       cancelButtonText: '取消',
  //       type: 'warning'
  //     }).then(action => {
  //     if (action === 'confirm') {
  //       localStorage.removeItem('tokenStr')
  //       router.push('/login')
  //     }
  //   }).catch(err => {
  //     if (err === 'cancel') {
  //       localStorage.setItem('logout', '1')
  //     }
  //   })
  // }
  // console.log(this)
  //   this.$message({
  //     message: '登录状态或密码已过期，请重新登录',
  //     type: 'error'
      
  //   })
      router.push('/login')
   }
}

export const postRequest = (url, data) => {
  return axios({
    url: `${url}`,
    method: 'post',
    data: data
  })
}

export const getRequest = (url, data) => {
  return axios({
    url: `${url}`,
    method: 'get',
    data: data
  })
}



export default service;